package top.yazif.text;

import top.yazif.excel.BigExcelImpl;
import top.yazif.excel.ExcelImpl;
import top.yazif.exception.ExcelException;

/**
 * Created by Yazif on 2017/3/25.
 */
public class ZLExcelImplText {
    public static void main(String[] args) throws Exception {


        BigExcelImpl excelMatrix  = new BigExcelImpl();

        ExcelImpl excel = new ExcelImpl("G:/temp/1.xlsx");
        ExcelImpl excelRelation = new ExcelImpl("G:/temp/2.xls");
        


        //纵坐标
        int rowYIndex = 0;
        int colYindex = 1;
        //横坐标
        int rowXIndex = 0;
        int colXIndex = 1;

        int count = excel.getNumberOfCell(rowYIndex); //矩阵excel的行数
        int relationCount = excelRelation.getNumberOfRows();// 关系Excek的行数

        for(int colIndex = 1;colIndex < count; colIndex ++){
            excelMatrix.setCellValue(excel.getValue(0, colIndex), 0,colIndex);
        }

        excelMatrix.setCellValue(excel.getValue(0,1),1 ,0);
        while (colYindex < count){//根据矩阵Excel的行列值取得权值，在关系Excel中读取相应权重并写入权值

            String valueX = excel.getValue(rowXIndex, colXIndex);//X坐标值
            String valueY = excel.getValue(rowYIndex, colYindex);//Y坐标值
            String value = "";


            int rowIndex = 0;//关系excel的行索引
            for(int colIndex = 0; rowIndex < relationCount; rowIndex ++){//找到关系Excel中的权重，没找到则为空
                String relationValue = excelRelation.getValue(rowIndex, colIndex);
                if(relationValue.equals(valueY + " " + valueX) || relationValue.equals(valueX + " " + valueY)){
                    value = excelRelation.getValue(rowIndex, colIndex + 1);
                    break;
                }
            }
            if(!"".equals(value)){//如果权重不为空，则写入矩阵
                excelMatrix.setCellValue(value, colYindex, colXIndex);
            }else {
                excelMatrix.setCellValue(String.valueOf(0),colYindex, colXIndex);
            }

            colXIndex++;
            if(colXIndex >= count){
                colYindex ++;
                colXIndex = 1;
                excelMatrix.setCellValue(excel.getValue(0, colYindex), colYindex, 0);
                System.out.println("now is "+ String.valueOf(colYindex));
            }
        }
        excelMatrix.save("G:/temp/3.xlsx");
        excelMatrix.close();
        excel.close();
        excelRelation.close();
    }
}
